// pages/dingwei_2/dingwei_2.js
const app=getApp()
var QQMapWX=require('../../utils/qqmap-wx-jssdk')
var qqmapsdk
Page({

  /**
   * 页面的初始数据
   */
  data: {
    province:'',
    address:'',
    lastitude:'',
    longotude:'',
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () 
  {
    var that=this
    var result=null;
    qqmapsdk=new QQMapWX({
      key:'UZABZ-6M6LX-V5E4Y-TK2BZ-I73ZJ-EWBKD'
    });
    wx.request({
      url: 'http://apis.juhe.cn/simpleWeather/query?',
      data:{
        "city":"成都",
        "key":"UZABZ-6M6LX-V5E4Y-TK2BZ-I73ZJ-EWBKD"
      },
      success(res){

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.getCurrentLocal()
    // let vm=this;
    // vm.getUserLocation();
    // 调用接口
    qqmapsdk.search({
        keyword: '酒店',
        success: function (res) {
            console.log(res);
        },
        fail: function (res) {
            console.log(res);
        },
    complete: function (res) {
        console.log(res);
    }
 });
},
getLocation:function(){
let vm=this;
wx.getLocation({
  altitude: true,
  type:'gcj02',
  success:function(res){
    console.log(JSON.stringify(res))
    var latitude=res.latitude
    var longitude=res.longitude
    var speed=res.accuracy;
    vm.getLocation(latitude,longitude)
  },
  fail:function(res){
    console.log('fail'+JSON.stringify(res))
  }
})
},
getCurrentLocal() {
  let that = this;
  wx.getSetting({
    success(res) {
      if (res.authSetting['scope.userLocation'] == false) { // 如果已拒绝授权，则打开设置页面
        wx.openSetting({
          success(res) {
            console.log(res)
          }
        })
      } else { // 第一次授权，或者已授权，直接调用相关api
        wx.getLocation({
          type: 'wgs84',
          success(res) {
            var param = {
              location: res.latitude + ',' + res.longitude,
              key: 'UZABZ-6M6LX-V5E4Y-TK2BZ-I73ZJ-EWBKD',
              get_poi: 1
            }
            //下面是get去请求数据
            var url = config.qqMapApi
            // https://apis.map.qq.com/ws/geocoder/v1/
            util.postrequest(url, param, function (res) {
              var d = res.data.result
              console.log(d)
              console.log(d.address_component.city)
              that.setData({
                citySelected: d.address_component.city,
              })
            })
          }
        })
      }
    }
  })
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})